package com.sheep.jucdemo.part4;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.stream.IntStream;

/**
 * @Description
 * @Author zaixianzhu
 * @Date 2022/8/2
 */
public class LongAccumulatorDemo {
    public static void main(String[] args) throws InterruptedException {
        LongAccumulator longAccumulator = new LongAccumulator((x, y) -> x + 2*y, 0);
        ExecutorService executorService = Executors.newFixedThreadPool(8);
        IntStream.range(1,10).forEach(i->executorService.submit(()->longAccumulator.accumulate(i)));
        Thread.sleep(2000);
        long rs = longAccumulator.getThenReset();
        System.out.println(rs);
    }
}
